package ernest.uicompare

import android.content.Context
import android.provider.Settings
import android.util.Log

object UICompare : IUICompare {
    private const val TAG = "UICompare"
    private var showing: Boolean = false
    private lateinit var config: Config
    private var context: Context?=null
    private var fileName = ""
    override fun init(context: Context, config: Config?) {
        this.context = context
        if (config == null) {
            this.config = ConfigBuilder().build()
        } else {
            this.config = config
        }
        FloatWindowManager.requestWindowPermission(UICompare.context)
    }

    override fun deInit(context: Context) {
        FloatWindowManager.removeBallView(UICompare.context)
    }

    override fun hideFloatWindow() {
        showing = false
        FloatWindowManager.removeBallView(context)
    }

    override fun showFloatWindow() {
        if (context?.hasDisplayWindowPermission() !=true) {
            Log.e(TAG, "has NO display window permission when showFloatWindow")
            return
        }

        if (showing) {
            return
        }
        showing = true
        FloatWindowManager.addBallView(context, floatWindowActionListener)

    }

    override fun setUI(fileName: String) {
        this.fileName = fileName
    }

    override fun showUI() {
        if (context?.hasDisplayWindowPermission() !=true) {
            Log.e(TAG, "has NO display window permission when showUI")
            return
        }
        UIWindowManager.addView(context, config.uiFileDir + "/" + fileName)
    }

    override fun showUI(fileName: String) {
        if (context?.hasDisplayWindowPermission() !=true) {
            Log.e(TAG, "has NO display window permission when showUI")
            return
        }
        UIWindowManager.addView(context, config.uiFileDir + "/" + fileName)
    }

    override fun hideUI() {
        UIWindowManager.removeView(context)
    }


    private var floatWindowActionListener = object : FloatWindow.FloatWindowActionListener {
        override fun onClick() {
            showUI()
        }

        override fun onLongClick() {

        }

    }
}
